package com.lohit.dbtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class AutocompleteActivity extends Activity {

	AutoCompleteTextView nameTextView;
	DbController controller = new DbController(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_autocomplete);

		nameTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteCountry);
		Button b1=(Button)findViewById(R.id.invisible);
		
		String[] patients = controller.getAllPatients();
		controller.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.list_item, patients);

		nameTextView.setAdapter(adapter);

	}

	public void createAppointment(View v) {

		String pName = nameTextView.getText().toString().trim();

		Long id = controller.getPatientId(pName);

		if (!(id == -1)) {
			String aid = String.valueOf(id);

			String phoneNumber = controller.getPatientNumber(aid);

			if (!(phoneNumber == null)) {

				Intent i = new Intent(this, CreateAppointmentActivity.class);

				i.putExtra("id", id);
				i.putExtra("phone", phoneNumber);
				Log.d("phone", phoneNumber);
				startActivity(i);

			}
			/*
			 * Intent i = new Intent(this, CreateAppointmentActivity.class);
			 * 
			 * i.putExtra("id", id);
			 * 
			 * startActivity(i);
			 */

		} else {

			nameTextView.setText("");

			nameTextView.requestFocus();

			Toast.makeText(this,
					"Patient details does not  exist ! Please try creating appoitment with new patient",
					Toast.LENGTH_SHORT).show();
			Intent i = new Intent(this, Main.class);
			startActivity(i);

		}

	}

	public void cancel(View v) {

		Intent i = new Intent(this, Main.class);
		
		nameTextView.setText("");
		
		startActivity(i);

	}
}
